/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JFrameMudarSenha.java
 *
 * Created on 03/12/2011, 22:11:46
 */
package gui;

import banco.pessoa.BancoFornecedor;
import banco.pessoa.BancoFuncionario;
import excecoes.estoque.DinheiroNegativoException;
import excecoes.pessoa.CpfCnpjExistenteException;
import excecoes.pessoa.CpfCnpjInvalidoException;
import excecoes.pessoa.LocalizacaoInvalidaException;
import excecoes.pessoa.NomeInvalidoException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import smartMarket.pessoa.Funcionario;

/**
 *
 * @author dyego
 */
public class JFrameMudarSenha extends javax.swing.JFrame {

    /** Creates new form JFrameMudarSenha */
    public JFrameMudarSenha() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelMudarSenha = new javax.swing.JPanel();
        jLabelCodigo = new javax.swing.JLabel();
        jLabelSenhaAtual = new javax.swing.JLabel();
        jLabelNovaSenha = new javax.swing.JLabel();
        jLabelConfirmarSenha = new javax.swing.JLabel();
        jPasswordFieldConfirmarSenha = new javax.swing.JPasswordField();
        jPasswordFieldNovaSenha = new javax.swing.JPasswordField();
        jPasswordFieldSenhaAtual = new javax.swing.JPasswordField();
        jTextFieldCodigo = new javax.swing.JTextField();
        jButtonOK = new javax.swing.JButton();
        jButtonCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanelMudarSenha.setBorder(javax.swing.BorderFactory.createTitledBorder("Mudar Senha"));

        jLabelCodigo.setText("Código:");

        jLabelSenhaAtual.setText("Senha Atual:");

        jLabelNovaSenha.setText("Nova Senha:");

        jLabelConfirmarSenha.setText("Confirmar Senha:");

        javax.swing.GroupLayout jPanelMudarSenhaLayout = new javax.swing.GroupLayout(jPanelMudarSenha);
        jPanelMudarSenha.setLayout(jPanelMudarSenhaLayout);
        jPanelMudarSenhaLayout.setHorizontalGroup(
            jPanelMudarSenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelMudarSenhaLayout.createSequentialGroup()
                .addGroup(jPanelMudarSenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelMudarSenhaLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanelMudarSenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanelMudarSenhaLayout.createSequentialGroup()
                                .addComponent(jLabelConfirmarSenha)
                                .addGap(10, 10, 10))
                            .addComponent(jLabelNovaSenha)
                            .addComponent(jLabelSenhaAtual)))
                    .addGroup(jPanelMudarSenhaLayout.createSequentialGroup()
                        .addGap(77, 77, 77)
                        .addComponent(jLabelCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelMudarSenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextFieldCodigo, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
                    .addComponent(jPasswordFieldConfirmarSenha, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
                    .addComponent(jPasswordFieldNovaSenha, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
                    .addComponent(jPasswordFieldSenhaAtual, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanelMudarSenhaLayout.setVerticalGroup(
            jPanelMudarSenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelMudarSenhaLayout.createSequentialGroup()
                .addGroup(jPanelMudarSenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelCodigo))
                .addGap(18, 18, 18)
                .addGroup(jPanelMudarSenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPasswordFieldSenhaAtual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelSenhaAtual))
                .addGap(18, 18, 18)
                .addGroup(jPanelMudarSenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPasswordFieldNovaSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelNovaSenha))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelMudarSenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPasswordFieldConfirmarSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelConfirmarSenha))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButtonOK.setText("OK");
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });

        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonCancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonOK, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanelMudarSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(15, Short.MAX_VALUE)
                .addComponent(jPanelMudarSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonOK)
                    .addComponent(jButtonCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
    BancoFuncionario bf = new BancoFuncionario();
    
    
        try {
            
            Funcionario funcionario = bf.buscar(Integer.parseInt(jTextFieldCodigo.getText()));
            boolean autentica = funcionario.autenticar(jPasswordFieldSenhaAtual.getText());
            
            if (autentica) {
                if (jPasswordFieldNovaSenha.getText().equals(jPasswordFieldConfirmarSenha.getText())) {
                   
                    funcionario.setSenha(jPasswordFieldNovaSenha.getText());
                    bf.alterar(funcionario);
                    this.setVisible(false);
                } else {
                    JOptionPane.showMessageDialog(null, "Senhas não coincidem");
                }
            } else {
                JOptionPane.showMessageDialog(null, "Erro durante a autenticação");
            }
            
            
            
        } catch (SQLException ex) {
            Logger.getLogger(JFrameMudarSenha.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NullPointerException ex) {
            Logger.getLogger(JFrameMudarSenha.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NomeInvalidoException ex) {
            Logger.getLogger(JFrameMudarSenha.class.getName()).log(Level.SEVERE, null, ex);
        } catch (CpfCnpjInvalidoException ex) {
            Logger.getLogger(JFrameMudarSenha.class.getName()).log(Level.SEVERE, null, ex);
        } catch (DinheiroNegativoException ex) {
            Logger.getLogger(JFrameMudarSenha.class.getName()).log(Level.SEVERE, null, ex);
        } catch (CpfCnpjExistenteException ex) {
            Logger.getLogger(JFrameMudarSenha.class.getName()).log(Level.SEVERE, null, ex);
        } catch (LocalizacaoInvalidaException ex) {
            Logger.getLogger(JFrameMudarSenha.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NumberFormatException nfe) {
            JOptionPane.showMessageDialog(null, "Erro no formato do código");
        }

}//GEN-LAST:event_jButtonOKActionPerformed

private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
    this.setVisible(false);
}//GEN-LAST:event_jButtonCancelarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonOK;
    private javax.swing.JLabel jLabelCodigo;
    private javax.swing.JLabel jLabelConfirmarSenha;
    private javax.swing.JLabel jLabelNovaSenha;
    private javax.swing.JLabel jLabelSenhaAtual;
    private javax.swing.JPanel jPanelMudarSenha;
    private javax.swing.JPasswordField jPasswordFieldConfirmarSenha;
    private javax.swing.JPasswordField jPasswordFieldNovaSenha;
    private javax.swing.JPasswordField jPasswordFieldSenhaAtual;
    private javax.swing.JTextField jTextFieldCodigo;
    // End of variables declaration//GEN-END:variables
}
